package com.website.company.controller;


import com.website.company.entity.Person;
import com.website.company.entity.Product;
import com.website.company.service.PersonService;
import com.website.company.service.ProductFunctionRelationService;
import com.website.company.service.ProductService;
import com.website.company.utils.PersonVO;
import com.website.company.utils.RelationDTO;
import com.website.company.utils.ResponseBo;
import com.website.company.utils.ResponseList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author peng yangyan
 * @since 2018-11-22
 */
@RestController
@RequestMapping("/api/person")
public class PersonController {

    @Autowired
    PersonService personService;
    @Autowired
    ProductFunctionRelationService productFunctionRelationService;

    @GetMapping("/getPerson")
    ResponseBo getPerson(@RequestParam("id") Integer id) {
        try {
            PersonVO personVO = new PersonVO();
            Person person = personService.show(id);
            BeanUtils.copyProperties(person, personVO, "functionId");
           // List<Integer> functionIds = productFunctionRelationService.getFunctionIds(product.getId());
            //personVO.setFunctionId(functionIds);
            return ResponseBo.ok(personVO);
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    @GetMapping("/getList")
    ResponseList<Person> getList(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                  @RequestParam("keyword") String keyword, @RequestParam("productClassName") String productClassName) {
        return personService.list(page, rows, keyword, productClassName);
    }

    @PostMapping("/delete")
    ResponseBo delete(@RequestBody Integer[] ids) {
        try {
            personService.delete(ids);
            return ResponseBo.ok();
        } catch (Exception e) {
            return ResponseBo.error(e.getMessage());
        }
    }

    @PostMapping("/savePerson")
    ResponseBo savePerson(@RequestBody PersonVO productVO) {
        if (productVO.getId() == null) {
            try {
                Person product = new Person();
                BeanUtils.copyProperties(productVO, product, "functionId");
                personService.add(product);
                return ResponseBo.ok(product);
            } catch (Exception e) {
                return ResponseBo.error(e.getMessage());
            }
        } else {
            try {
                Person product = new Person();
                BeanUtils.copyProperties(productVO, product);
                personService.update(product);
                return ResponseBo.ok(product);
            } catch (Exception e) {
                return ResponseBo.error(e.getMessage());
            }
        }
    }

}
